<!-- 报告单元(rm-ddl-rptunit)，报告项目(rm-ddl-rptitem)，项目分类(rm-ddl-rptitemclass)，标本类型(rm-ddl-specimen) -->
<template>
  <div style="height:100%;width:100%;position:relative;">
    <div class="pagetop">
      <div style="width:100%;">
        <el-scrollbar style="width:100%;height:100%">
          <div ref="buttondiv">
            <vxe-toolbar class="table1style">
              <template v-slot:buttons>
                <table>
                  <tr>
                    <td>分单类别</td>
                    <td>
                      <rm-ddl-common :dataname="groupname" :datatype="grouptype"
                        @change="all_selectChange_event($event, 'el_group')" appendtobody style="width:200px"
                        v-model="reqgroup_model"></rm-ddl-common>
                    </td>
                    <td>所属单元</td>
                    <td>
                      <rm-ddl-rptunit prgno="LIS113" @change="all_selectChange_event($event, 'el_unit')" appendtobody
                        ref="select_rptunit_controls" style="width:200px" v-model="rptunit_model"></rm-ddl-rptunit>
                    </td>
                    <td v-if="!isbis">报告项目类别</td>
                    <td v-if="!isbis">
                      <rm-ddl-rptitemclass @change="all_selectChange_event($event, 'el_rptitemclass')" appendtobody
                        size="small" style="width:200px" v-model="rptitemclass_model"></rm-ddl-rptitemclass>
                    </td>
                    <td>
                      <el-checkbox v-model="show_right" @change="col_show_hide">外部代码模式</el-checkbox>
                    </td>
                    <td>
                      <el-checkbox v-model="show_remind" @change="col_del_remind">删除对照不提醒</el-checkbox>
                    </td>
                    <td :width="buttonwidth"></td>
                    <td>
                      <el-button @click="all_clear" icon="el-icon-circle-close" size="small">清空</el-button>
                    </td>
                  </tr>
                </table>
              </template>
            </vxe-toolbar>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div class="pagecenter">
      <rm-layout-left splitcolor="white" movingcolor="#00ff00" :splitwidth=5 :fixsize=500>
        <template v-slot:fix>
          <div>
            <div style="border: solid 1px #ccc;padding:3px;text-align:left;">
              <el-input placeholder="申请项目代码|名称|输入码" prefix-icon="el-icon-search" size="small" style="max-width:200px;"
                v-model="item_dict_seachKey" @keyup.enter.native="item_dict_refresh" ref="leftsearchinput"
                @focus="SelectAll('left')"></el-input>
              <el-button @click="item_dict_refresh" icon="el-icon-search" type="primary">查询</el-button>
            </div>
            <div class="child_bottom">
              <vxe-table :data.sync="item_dict_tableData" :loading="item_dict_loading" :select-config="{ reserve: true }"
                :sort-config="{ trigger: 'cell' }" :edit-config="{ showIcon: false, activeMethod: cellactiveMethod }"
                header-align="center" @cell-dblclick="showDurationFinishReport"
                @current-change="item_dict_currentChangeEvent" border height="auto" highlight-current-row
                highlight-hover-column highlight-hover-row how-header-overflow ref="item_dict_Table" resizable
                row-id="item_dict_Table_rowid" show-overflow size="mini" stripe>
                <vxe-table-column field="req_groupname" sortable title="分单类别" width="70"></vxe-table-column>
                <vxe-table-column field="req_itemcode" sortable title="项目代码" width="78"></vxe-table-column>
                <vxe-table-column field="req_itemname" sortable title="项目名称" width="170"></vxe-table-column>
                <vxe-table-column field="item_price" sortable title="单价" width="50" header-align="center"
                  align="right"></vxe-table-column>
                <vxe-table-column field="specimen_name" sortable title="默认标本类型" width="90"></vxe-table-column>
                <vxe-table-column field="his_itemcode" sortable title="HIS代码" width="78"></vxe-table-column>
                <vxe-table-column field="cp_div" sortable title="拆分" width="40">
                </vxe-table-column>
                <vxe-table-column field="cp_add" sortable title="附加" width="40">
                </vxe-table-column>
                <vxe-table-column field="perform_dept" sortable title="执行科室" width="80"></vxe-table-column>
                <vxe-table-column field="inputcode" sortable title="拼音码" width="80"></vxe-table-column>
                <vxe-table-column field="itemclasscode" sortable title="项目归类" width="80"></vxe-table-column>
                <vxe-table-column field="hospcode" sortable title="所属院区" width="100"
                  :edit-render="{ name: 'select', options: HospitalOptions }"></vxe-table-column>
              </vxe-table>
            </div>
          </div>
        </template>
        <rm-layout-right splitcolor="white" movingcolor="#0000ff" :splitwidth=5 :fixsize=middle_center :key="pagekey">
          <template v-slot:fix>
            <div>
              <div style="border: solid 1px #ccc;padding:3px;">
                <el-input placeholder="报告项目代码|名称|缩写|输入码" prefix-icon="el-icon-search" size="small"
                  style="max-width:200px;" v-model="rptitem_seachKey" @keyup.enter.native="rptitem_refresh"
                  ref="rightsearchinput" @focus="SelectAll('right')"></el-input>
                <el-button @click="rptitem_refresh" icon="el-icon-search" type="primary">查询</el-button>
              </div>
              <div class="child_bottom">
                <vxe-table :data.sync="rptitem_tableData" :loading="rptitem_loading" :select-config="{ reserve: true }"
                  :sort-config="{ trigger: 'cell' }" @cell-dblclick="rptitem_add_Eevent" border height="auto"
                  highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow
                  header-align="center" ref="rptitem_Table" resizable row-id="rptitem_Table_rowid" show-overflow
                  size="mini" stripe>
                  <vxe-table-column field="itemcode_en" sortable title="英文缩写" width="70"></vxe-table-column>
                  <vxe-table-column field="rpt_itemname" sortable title="报告项目名称" width="110"></vxe-table-column>
                  <vxe-table-column field="itemclassname" sortable title="项目归类" width="70"></vxe-table-column>
                  <vxe-table-column field="instrclass" sortable title="仪器类别" width="70"></vxe-table-column>
                  <vxe-table-column field="rpt_itemcode" sortable title="报告项目代码" width="90"></vxe-table-column>
                  <vxe-table-column field="seq" sortable title="打印序号" width="70"></vxe-table-column>
                </vxe-table>
              </div>
            </div>
          </template>

          <div style="height:100%;width:100%;">
            <div style="border: solid 1px #ccc;padding:3px;">
              <el-scrollbar style="width:100%;height:100%">
                <vxe-toolbar :refresh="{ query: reqitem_rptitem_refresh }" style="minWidth:435px;">
                  <template v-slot:buttons>
                    <el-button @click="reqitem_rptitem_SaveEvent()" :disabled="reqitem_rptitem_saveBtnbool"
                      style="margin-right:2px">保存外部代码</el-button>
                    <el-button-group>
                      <!-- <el-button @click="rptitem_delete_Eevent" ref="reqitem_rptitem_delBtn" v-bind:disabled="reqitem_rptitem_delBtnbool" style="margin-right:2px">删除</el-button> -->
                      <el-button @click="Copy()" style="margin-right:2px">复制</el-button>
                      <el-button @click="Stick()" style="margin-right:2px">粘贴</el-button>
                    </el-button-group>

                    <!-- <vxe-tooltip content="外部代码模式">
                                      <el-switch v-model="show_right" @change="col_show_hide" style="margin-left:10px;">
                                      </el-switch>
                                  </vxe-tooltip>
                                  <vxe-tooltip content="删除对照不提醒">
                                      <el-switch v-model="show_remind" @change="col_del_remind" style="margin-left:10px;">
                                      </el-switch>
                                  </vxe-tooltip> -->
                  </template>
                </vxe-toolbar>
              </el-scrollbar>
            </div>
            <div class="child_bottom">
              <vxe-table :data.sync="reqitem_rptitem_tableData" :loading="reqitem_rptitem_loading"
                :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border height="auto"
                highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow
                ref="reqitem_rptitem_table" resizable row-id="his_interface_table_rowid" show-overflow size="mini" stripe
                :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: false }" header-align="center"
                @cell-dblclick="reqitem_rptitem_dblclick" :row-class-name="rpt_req_rowclassname" show-header-overflow
                :optimization="{ animat: false }">
                <vxe-table-column type="index" width="34" title="NO." align="center"></vxe-table-column>
                <vxe-table-column field="rpt_itemname" sortable title="对应报告项目" width="120"></vxe-table-column>
                <vxe-table-column field="seq" sortable title="序号" width="40" align="center"></vxe-table-column>
                <vxe-table-column align="center" field="optional" sortable title="可选" width="40">
                  <template v-slot="{ row }">
                    <el-checkbox v-model="row.optional" true-label="1" false-label="0"
                      @change="reqitem_rptitem_column_OptionalChange"></el-checkbox>
                  </template>
                </vxe-table-column>
                <vxe-table-column align="center" field="usedetail" sortable title="标本|单元相关" width="90">
                  <template v-slot="{ row }">
                    <el-checkbox v-model="row.usedetail" true-label="1" false-label="0"
                      @change="reqitem_rptitem_column_usedetailChange(row)"></el-checkbox>
                  </template>
                </vxe-table-column>
                <vxe-table-column align="center" field="cnt" sortable title="详情(双击开窗)" width="70">
                </vxe-table-column>
                <vxe-table-column field="externalcode1" sortable title="外部代码1(双击开窗)" width="120" v-if="show_right"
                  :edit-render="{ name: 'input', autoselect: true }">
                </vxe-table-column>
                <vxe-table-column field="externalcode2" sortable title="外部代码2(双击开窗)" width="120" v-if="show_right"
                  :edit-render="{ name: 'input', autoselect: true }">
                </vxe-table-column>
                <vxe-table-column field="externalcode3" sortable title="外部代码3(双击开窗)" width="120" v-if="show_right"
                  :edit-render="{ name: 'input', autoselect: true }">
                </vxe-table-column>
                <!-- <vxe-table-column field="req_itemcode" sortable title="申请项目代码" width="80"></vxe-table-column> -->
                <vxe-table-column field="rpt_itemcode" sortable title="报告项目代码" width="80"></vxe-table-column>
                <vxe-table-column v-if="rptunit_model" field="ismatchrptunit" sortable title="已对照当前报告单元" width="120"
                  align="center">
                  <template v-slot="{ row }">
                    <span v-if="row.ismatchrptunit == '1'">是</span>
                    <span v-else></span>
                  </template>
                </vxe-table-column>
              </vxe-table>
            </div>
          </div>
        </rm-layout-right>
      </rm-layout-left>
    </div>
    <el-dialog :visible.sync="dialogTable_externalcode" @close="externalcode_tableData_close" ref="qweqweqwe"
      title="外部代码段" width="642px" v-el-drag-dialog :close-on-click-modal="false">
      <vxe-toolbar style="margin-bottom:5px;">
        <template v-slot:buttons>
          <el-input class="input-with-select" placeholder="输入查询内容" prefix-icon="el-icon-search" size="small"
            style="max-width:150px;" v-model="externalcode_seach"></el-input>
          <el-button @click="externalcode_seachEvent" icon="el-icon-search" size="small" slot="append"
            type="primary">查询</el-button>
        </template>
      </vxe-toolbar>
      <vxe-table :data.sync="externalcode_tableData" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }"
        @cell-dblclick="externalcode_tableData_dbclick($event)" border height="300" highlight-current-row
        highlight-hover-column highlight-hover-row how-header-overflow resizable show-overflow size="mini" stripe
        header-align="center">
        <vxe-table-column field="rptitemcode" sortable title="项目代码" width="100"></vxe-table-column>
        <vxe-table-column field="rptitemname" sortable title="项目名称" width="100"></vxe-table-column>
        <vxe-table-column field="comment1" sortable title="备用字段1" width="100"></vxe-table-column>
        <vxe-table-column field="comment2" sortable title="备用字段2" width="100"></vxe-table-column>
        <vxe-table-column field="inputcode" sortable title="助记符" width="100"></vxe-table-column>
        <vxe-table-column field="externalmode" sortable title="来源类别" width="100"></vxe-table-column>
      </vxe-table>
    </el-dialog>

    <el-dialog :visible.sync="dialogFinshReport" @close="dialogFinshReportclose" ref="qweqweqwe" title="报告项目选择"
      width="642px" v-el-drag-dialog :close-on-click-modal="false">
      <vxe-toolbar style="margin-bottom:5px;">
        <template v-slot:buttons>
          <el-input-number size="small" style="max-width:150px;" v-model="timespan"
            @keyup.enter.native="QueryDurationFinishReport"></el-input-number>天内报告数据
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <el-button @click="QueryDurationFinishReport" icon="el-icon-search" size="small" slot="append"
            type="primary">查询</el-button>
          <el-button @click="dialogFinshReportclose" icon="el-icon-close" size="small" slot="append">退出</el-button>
        </template>
      </vxe-toolbar>
      <vxe-table :data.sync="FinshReportData" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border
        height="300" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow resizable
        show-overflow size="mini" stripe header-align="center">
        <vxe-table-column field="sampledate" sortable title="标本日期" width="80"
          :formatter="['toDateString', 'yyyy-MM-dd']"></vxe-table-column>
        <vxe-table-column field="sampleno" sortable title="标本号" width="60"></vxe-table-column>
        <vxe-table-column field="ischk" sortable title="选择" width="40" align="center">
          <template v-slot="{ row }">
            <el-checkbox v-model="row.ischk" true-label="1" false-label="0" :disabled="row.chkflag == '0'"
              @change="FinshReportChk(row)"></el-checkbox>
          </template>
        </vxe-table-column>
        <vxe-table-column field="itemcode_en" sortable title="英文缩写" width="70"></vxe-table-column>
        <vxe-table-column field="rpt_itemname" sortable title="项目名称" width="110"></vxe-table-column>
        <vxe-table-column field="result_str" sortable title="检验结果" width="70"></vxe-table-column>
        <vxe-table-column field="rptunitname" sortable title="报告单元" width="110"></vxe-table-column>
      </vxe-table>
    </el-dialog>

    <el-dialog :visible.sync="dialogdetail" title="详情" width="520px" v-el-drag-dialog :close-on-click-modal="false">
      <vxe-table :data.sync="detailData" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border
        height="300" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow resizable
        show-overflow size="mini" stripe header-align="center" ref="detailXtable"
        :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: false }">
        <vxe-table-column field="seq" sortable title="序号" width="60" align="center"></vxe-table-column>
        <vxe-table-column field="specimen_code" sortable title="标本类型" width="200"
          :edit-render="{ name: 'select', options: specimen_codeoption }">
        </vxe-table-column>
        <vxe-table-column field="rptunitid" sortable title="报告单元" width="200"
          :edit-render="{ name: 'select', options: rptunitidoption }">
          <!-- <template v-slot="{row}">
                      <rm-ddl-rptunit prgno="LIS113" v-model="row.rptunitid" width="100%"></rm-ddl-rptunit>
                  </template> -->
        </vxe-table-column>
      </vxe-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogbtnAdd" icon="el-icon-plus" size="small" slot="append">新增</el-button>
        <el-button @click="dialogbtnDel" icon="el-icon-delete" size="small" slot="append">删除</el-button>
        <el-button @click="savedetail" icon="el-icon-document" size="small" slot="append">确定</el-button>
        <el-button @click="dialogdetail = false" icon="el-icon-close" size="small" slot="append">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :title="headtitle"
      :visible.sync="dialogVisible">
      <rm-pop-common ref="popcom" @change="popcomchange" @cancel="dialogVisible = false;"
        :datatype="datatype"></rm-pop-common>
    </el-dialog>
  </div>
</template>

<script>
import { Promise } from "q";
import { log } from "util";
import { max } from "moment";
import XE from "xe-utils"; //引入工具类
export default {
  name: "LIS113",
  data() {
    return {
      //全局
      middle_center: 405,
      pagekey: 0,
      copyflag: 0,
      buttonwidth: "",
      groupname: "分单类别",
      grouptype: "reqgroup",
      show_right: false, //外部代码模式
      show_remind: false, //删除对照不提醒
      reqgroup_model: "", //项目分单类别
      rptunit_model: "", //报告所属单元select model
      rptitemclass_model: "", //报告项目类别 select model;
      ////////  req_item_dict 申请项目 数据变量声明  /////////
      item_dict_loading: false,
      item_dict_tableData: [], //req_item_dict 表数据容器(界面展示)
      item_dict_currentRow: [], //req_item_dict 点击数据行获取数据容器
      item_dict_seachKey: "", //查询条件
      ////////  lab_reqitem_rptitem 对应表 数据变量声明  //////////
      reqitem_rptitem_loading: false,
      reqitem_rptitem_tableData: [],
      stickdata: [],
      copydatalen: 0,
      dialogdetail: false,
      detailData: [],
      reqitem_rptitem_delBtnbool: true,
      reqitem_rptitem_saveBtnbool: true,
      externalcode_tableData: [], //界面展示数据
      externalcode_tableDataLog: [], //AJAX真实获取数据
      externalcode_seach: "", //外部代码查询条件
      dialogTable_externalcode: false, //外部代码弹出框控制标志
      externalcode_rowIndex: "", //打开外部代码弹出框 记录行号
      externalcode_flag: "", //打开外部代码弹出框 记录外部代码 1/2/3
      ////////  lab_rptitem 对应表 数据变量声明  //////////
      rptitem_loading: false,
      rptitem_tableData: [], //lab_rptitem 表数据容器(界面展示)
      rptitem_seachKey: "", //查询条件
      okFirstLoading: false,
      HospitalOptions: [],
      utype: this.$tool.getSession(this.$tool.loginutype),
      dialogFinshReport: false,
      dialogVisible: false,
      timespan: 10,
      FinshReportData: [],
      RowDetail: [],
      rptunitidoption: [],
      specimen_codeoption: [],
      datatype: "",
      headtitle: "",
      poprow_specimen: [],
      isbis: false,
    };
  },
  created() {
    this.isbis = this.$tool.checkBis();
    this.ddlInitdata();
    this.initOption();
    this.item_dict_refresh();
    this.rptitem_refresh();
    this.rexternalmode_refresh();
  },
  mounted() { },
  updated() {
    this.getButtonWidth();
    if (this.okFirstLoading) {
      let currentrow = this.$refs.item_dict_Table.getData(0);
      if (currentrow) {
        this.$nextTick(() => {
          this.okFirstLoading = false;
          this.$refs.item_dict_Table.setCurrentRow(currentrow);
          this.item_dict_currentChangeEventFun(currentrow);
        });
      }
    }
  },
  methods: {
    initOption() {
      this.$tool
        .request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "initOption",
          },
        })
        .then((res) => {
          this.rptunitidoption = res.dt2;
          this.specimen_codeoption = res.dt1;
        });
    },
    ddlInitdata() {
      this.$tool
        .request({
          url: "MNG/MNG2/MNG215",
          method: "post",
          data: {
            action: "ddlInitialList",
          },
        })
        .then((response) => {
          this.HospitalOptions = response;
        });
    },
    cellactiveMethod({ row, column }) {
      if (["externalcode1", "externalcode2", "externalcode3"].indexOf(column.property) > -1) {
        return true;
      } else {
        return false;
      }
    },
    /////////////////////////////////////   全局    /////////////////////////////////////////////
    //控制el-col 宽度
    col_show_hide() {
      //this.show_right = !this.show_right;
      if (this.show_right) {
        this.middle_center = 0;
      } else {
        this.middle_center = 405;
      }
      this.pagekey++;
    },
    //删除对照不提醒
    col_del_remind() {
      this.show_remind == false ? true : false;
    },
    //select 数据变化事件集合
    all_selectChange_event(evt, flag) {
      if (flag === "el_group") {
        this.item_dict_refresh();
      }
      if (flag === "el_unit") {
        this.rptitem_refresh();
      }
      if (flag === "el_rptitemclass") {
        this.rptitem_refresh();
      }
    },
    getButtonWidth() {
      if (this.$refs.buttondiv) {
        this.buttonwidth = this.$refs.buttondiv.parentElement.parentElement.parentElement.parentElement.offsetWidth - 200 + "px";
      }
    },
    //清空条件
    all_clear() {
      this.reqgroup_model = "";
      this.rptunit_model = "";
      this.rptitemclass_model = "";
      this.item_dict_seachKey = "";
      this.rptitem_seachKey = "";
      this.item_dict_refresh();
      this.rptitem_refresh();
    },
    /////////////////////////////////////   req_item_dict   /////////////////////////////////////////////
    //req_item_dict数据获取/刷新方法 AJAX
    item_dict_refresh() {
      this.item_dict_loading = true;
      let _this = this;
      this.$tool
        .request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "item_dict_query",
            selectkey: _this.item_dict_seachKey.trim(" "),
            groupid: _this.reqgroup_model,
          },
        })
        .then((res) => {
          this.item_dict_tableData = res.Data;
          this.item_dict_loading = false;
          this.okFirstLoading = true;
          if (this.item_dict_tableData.length > 0) {
            this.$nextTick(() => {
              let currentrow = this.$refs.item_dict_Table.getData(0);
              this.$refs.item_dict_Table.setCurrentRow(currentrow);
              this.item_dict_currentChangeEventFun(currentrow);
            })
          }
          else {
            this.reqitem_rptitem_tableData = [];
          }
        })
        .catch((e) => {
          this.$message.error(e);
        });
    },
    //req_item_dict 数据表格 行获取事件
    item_dict_currentChangeEvent({ row }) {
      this.item_dict_currentChangeEventFun(row);
    },
    item_dict_currentChangeEventFun(row) {
      this.item_dict_currentRow = row;
      this.reqitem_rptitem_refresh(); //获取对照关系表
    },
    ////////////////////////////////////    lab_reqitem_rptitem   //////////////////////////////////////////////
    //lab_reqitem_rptitem 数据获取/刷新方法 AJAX
    reqitem_rptitem_refresh() {
      this.reqitem_rptitem_loading = true;
      this.$tool
        .request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "reqitem_rptitem_query",
            req_itemcode: this.item_dict_currentRow.req_itemcode,
            rptunitid: this.rptunit_model,
          },
        })
        .then((res) => {
          this.reqitem_rptitem_tableData = res.Data;
          this.reqitem_rptitem_loading = false;
          this.reqitem_rptitem_dataChanged();
        })
        .catch((e) => {
          console.log("erro", e);
        });
    },
    //externalcode_refresh 数据获取/刷新方法
    rexternalmode_refresh() {
      this.$tool
        .request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "get_externalmode",
          },
        })
        .then((res) => {
          this.externalcode_tableDataLog = res.Data;
          this.externalcode_tableData = JSON.parse(JSON.stringify(res.Data[0]));
        })
        .catch((e) => {
          this.$message.error(e);
        });
    },
    //弹出外部代码框
    open_externalcodeDiv(rowIndex, columnindex, column) {
      const nameArr = ["externalcode1", "externalcode2", "externalcode3"];
      if (nameArr.includes(column.property)) {
        this.externalcode_seach = "";
        this.externalcode_rowIndex = rowIndex;
        this.externalcode_flag = column.property;
        this.externalcode_tableData.splice(0, this.externalcode_tableData.length);
        if (column.property == "externalcode1") {
          this.externalcode_tableData = JSON.parse(JSON.stringify(this.externalcode_tableDataLog[0]));
        }
        if (column.property == "externalcode2") {
          this.externalcode_tableData = JSON.parse(JSON.stringify(this.externalcode_tableDataLog[1]));
        }
        if (column.property == "externalcode3") {
          this.externalcode_tableData = JSON.parse(JSON.stringify(this.externalcode_tableDataLog[2]));
        }
        this.dialogTable_externalcode = true;
      }
    },
    //外部代码 双击选择 赋值事件
    externalcode_tableData_dbclick(evt) {
      if (this.externalcode_flag === "externalcode1") {
        //externalcode1
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode1 = evt.row.rptitemcode;
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode1_name = evt.row.rptitemname;
      }
      if (this.externalcode_flag === "externalcode2") {
        //externalcode2
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode2 = evt.row.rptitemcode;
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode2_name = evt.row.rptitemname;
      }
      if (this.externalcode_flag === "externalcode3") {
        //externalcode3
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode3 = evt.row.rptitemcode;
        this.reqitem_rptitem_tableData[this.externalcode_rowIndex].externalcode3_name = evt.row.rptitemname;
      }
      this.dialogTable_externalcode = false;
    },
    externalcode_tableData_close() {
      this.externalcode_seach = "";
      this.externalcode_rowIndex = "";
      this.externalcode_flag = "";
    },

    //externalcode_seach 前端查询
    externalcode_seachEvent() {
      let seachstr = this.externalcode_seach;
      this.externalcode_tableData.splice(0, this.externalcode_tableData.length);
      //清空数组
      if (this.externalcode_flag === "externalcode1") {
        for (let i = 0; i < this.externalcode_tableDataLog[0].length; i++) {
          if (this.externalcode_tableDataLog[0][i].rptitemcode.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[0][i].rptitemname.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[0][i].inputcode.indexOf(seachstr) >= 0) {
            this.externalcode_tableData.push(this.externalcode_tableDataLog[0][i]);
          }
        }
      }
      if (this.externalcode_flag === "externalcode2") {
        for (let i = 0; i < this.externalcode_tableDataLog[1].length; i++) {
          if (this.externalcode_tableDataLog[1][i].rptitemcode.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[1][i].rptitemname.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[1][i].inputcode.indexOf(seachstr) >= 0) {
            this.externalcode_tableData.push(this.externalcode_tableDataLog[1][i]);
          }
        }
      }
      if (this.externalcode_flag === "externalcode3") {
        for (let i = 0; i < this.externalcode_tableDataLog[2].length; i++) {
          if (this.externalcode_tableDataLog[2][i].rptitemcode.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[2][i].rptitemname.indexOf(seachstr) >= 0 || this.externalcode_tableDataLog[2][i].inputcode.indexOf(seachstr) >= 0) {
            this.externalcode_tableData.push(this.externalcode_tableDataLog[2][i]);
          }
        }
      }
    },
    //lab_reqitem_rptitem 删除
    rptitem_delete_Eevent(delrow) {
      if (this.$ddldata.IsAreaMode && this.utype != "G" && this.item_dict_currentRow.hospcode == "All") {
        this.$alert("您不是管理员，不能对医院代码为All对应的数据做删除！", "提示", {
          confirmButtonText: "确定",
        });
        return false;
      }
      if (delrow) {
        if (!this.show_remind) {
          //提醒
          this.$confirm("此操作将删除选中的数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              //删除
              // this.$refs.reqitem_rptitem_table.removeSelecteds();
              this.delRecordsSave("", delrow);
            })
            .catch(() => { });
        } else {
          //不提醒
          //this.$refs.reqitem_rptitem_table.removeSelecteds();
          this.delRecordsSave("", delrow);
        }
      }
    },
    delRecordsSave(addrow, delrow) {
      this.save(addrow, delrow).then((res) => {
        if (res == "1") {
          this.$message({
            message: "删除成功！",
            type: "success",
            duration: "1000",
          });
          this.reqitem_rptitem_refresh();
        } else {
          this.$message.error("删除失败！" + res);
        }
      });
    },
    reqitem_rptitem_dblclick({ row, rowIndex, columnindex, column }) {
      if (["rpt_itemname", "seq", "req_itemcode", "rpt_itemcode"].includes(column.property)) {
        this.rptitem_delete_Eevent(row);
      } else if (column.property == "cnt") {
        if (row.usedetail == "1") {
          this.RowDetail = row;
          this.$tool
            .request({
              url: "LIS/LIS1/LIS113",
              method: "post",
              data: {
                action: "reqitem_rptitem_detail",
                reqitemcode: row.req_itemcode,
                rptitemcode: row.rpt_itemcode,
              },
            })
            .then((res) => {
              //刷新对应表
              if (res) {
                this.detailData = res.data;
                this.dialogdetail = true;
              } else {
                this.$message.error("查询失败！" + res);
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
        } else {
          this.$confirm("请先勾选标本或单元相关！", "信息");
        }
      } else {
        this.open_externalcodeDiv(rowIndex, columnindex, column);
      }
    },
    //保存数据 保存
    reqitem_rptitem_SaveEvent() {
      if (this.$ddldata.IsAreaMode && this.utype != "G" && this.item_dict_currentRow.hospcode == "All") {
        this.$alert("您不是管理员，不能对医院代码为All对应的数据做保存！", "提示", {
          confirmButtonText: "确定",
        });
        return false;
      }

      let Alldata = this.$refs.reqitem_rptitem_table.getRecordset();
      let GetDelData = Alldata.removeRecords;
      let GetUpData = Alldata.updateRecords;
      let insertRecords = Alldata.insertRecords;
      if ((GetDelData == undefined || GetDelData.length <= 0) && (GetUpData == undefined || GetUpData.length <= 0) && (insertRecords == undefined || insertRecords.length <= 0)) {
        this.$message({
          message: "数据未发生变化！",
          type: "warning",
        });
        return false;
      }
      //存入数据库
      this.$tool
        .request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "reqitem_rptitem_save",
            delData: GetDelData,
            upData: GetUpData,
            insertData: insertRecords,
          },
        })
        .then((res) => {
          //刷新对应表
          if (res == "1") {
            this.$pub.message(this, "保存成功！", "success");
            this.reqitem_rptitem_refresh();
          } else {
            this.$message.error("保存失败！" + res);
          }
        })
        .catch((e) => {
          this.$message.error(e);
        });
    },
    //lab_reqitem_rptitem 数据变化事件
    reqitem_rptitem_dataChanged() {
      if (this.reqitem_rptitem_tableData.length == 0) {
        this.reqitem_rptitem_saveBtnbool = true;
      } else {
        this.reqitem_rptitem_saveBtnbool = false;
      }
    },
    //可选check改变
    reqitem_rptitem_column_OptionalChange(rowIndex) {
      this.reqitem_rptitem_SaveEvent();
    },
    //标本或类型相关check改变
    reqitem_rptitem_column_usedetailChange(row) {
      if (row.usedetail == "1") {
        this.reqitem_rptitem_SaveEvent();
      } else {
        if (this.$ddldata.IsAreaMode && this.utype != "G" && this.item_dict_currentRow.hospcode == "All") {
          this.$alert("您不是管理员，不能对医院代码为All对应的数据做保存！", "提示", {
            confirmButtonText: "确定",
          });
          return false;
        }
        this.$confirm("取消相关将删除相关数据，你确定取消吗？", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$tool
              .request({
                url: "LIS/LIS1/LIS113",
                method: "post",
                data: {
                  action: "delreqitem_rptitem_detail",
                  reqitemcode: row.req_itemcode,
                  rptitemcode: row.rpt_itemcode,
                },
              })
              .then((res) => {
                this.reqitem_rptitem_SaveEvent();
              })
              .catch((e) => {
                this.$message.error(e);
              });
          })
          .catch(() => {
            row.usedetail = "1";
          });
      }
    },
    SelectAll(searchtype) {
      if (searchtype == 'left') this.$refs.leftsearchinput.select();
      else this.$refs.rightsearchinput.select();
    },
    //lab_rptitem数据获取/刷新方法 AJAX
    rptitem_refresh() {
      this.$nextTick(() => {
        this.rptitem_loading = true;
        let _this = this;
        this.$tool
          .request({
            url: "LIS/LIS1/LIS113",
            method: "post",
            data: {
              action: "rptitem_query",
              selectkey: _this.rptitem_seachKey.trim(" "),
              rptunit: _this.rptunit_model,
              rptitemclass: _this.rptitemclass_model,
            },
          })
          .then((res) => {
            this.rptitem_tableData = res.Data;
            this.rptitem_loading = false;
          })
          .catch((e) => {
            this.$message.error(e);
          });
      });
    },
    //lab_rptitem 数据表格 行获取事件 右边
    rptitem_add_Eevent({ row, rowIndex, column, columnIndex }, event) {
      if (this.item_dict_currentRow == undefined || this.item_dict_currentRow.length <= 0) {
        this.$message({
          message: "请先确认申请项目！",
          type: "warning",
        });
        return false;
      }
      if (this.$ddldata.IsAreaMode && this.utype != "G" && this.item_dict_currentRow.hospcode == "All") {
        this.$alert("您不是管理员，不能对医院代码为All的数据做对照！", "提示", {
          confirmButtonText: "确定",
        });
        return false;
      }

      this.AddReqItem_RptItem(row);
    },
    AddReqItem_RptItem(row) {
      let data = this.$refs.reqitem_rptitem_table.tableData;
      let fdata = data.filter((item) => item.rpt_itemcode == row.rpt_itemcode);
      if (fdata.length > 0) {
        this.$message.warning("该报告项目已存在！");
        return false;
      }
      let seqlist = XE.max(data, (item) => item.seq);
      let maxseq = 10;
      if (!!seqlist) {
        maxseq = seqlist.seq + 10;
      }

      let add_reqitem_rptitem_custom = {
        rpt_itemname: row.rpt_itemname,
        seq: maxseq,
        optional: 0, //添加时默认不勾选【可选】
        externalcode1: "",
        externalcode2: "",
        externalcode3: "",
        req_itemcode: this.item_dict_currentRow.req_itemcode,
        rpt_itemcode: row.rpt_itemcode,
      };

      // this.$refs.reqitem_rptitem_table.insert(add_reqitem_rptitem_custom);
      this.save(add_reqitem_rptitem_custom, "").then((res) => {
        if (res == "1") {
          this.$message({
            message: "保存成功！",
            type: "success",
            duration: "1000",
          });
          this.reqitem_rptitem_refresh();
        } else {
          this.$message.error("保存失败！" + res);
        }
      });
    },
    async save(insertRecord, DelRecord) {
      try {
        const res = await this.$tool.request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "reqitem_rptitem_save",
            delData: !DelRecord ? [] : [DelRecord],
            upData: [],
            insertData: !insertRecord ? [] : [insertRecord],
          },
        });
        return res;
      } catch (e) {
        console.log(e);
      }
    },
    //"rm-cancel"; //作废
    //"rm-disable"; //禁用
    //"rm-readonly"; //只读
    //"rm-alert"; //警告
    rpt_req_rowclassname({ row, rowIndex }) {
      if (row.optional == "1") {
        return "col-red"; //禁用
      } else if (this.rptunit_model) {
        if (row.ismatchrptunit != "1") {
          return "col-grey";
        }
      } else {
        return "";
      }
    },
    Copy() {
      this.copydata = JSON.parse(JSON.stringify(this.reqitem_rptitem_tableData));
    },
    Stick() {
      if (this.item_dict_currentRow == undefined || this.item_dict_currentRow.length <= 0) {
        this.$message({
          message: "请先确认申请项目！",
          type: "warning",
        });
        return false;
      }

      var maxseq = 0;
      var insertRecords = this.$refs.reqitem_rptitem_table.getInsertRecords();
      if (insertRecords.length > 0) {
        //后添加的数据中遍历最大SEQ
        maxseq = Number(insertRecords[0].seq);
      } else {
        if (this.reqitem_rptitem_tableData.length > 0) {
          //数据库中数据遍历最大SEQ
          maxseq = Number(this.reqitem_rptitem_tableData[this.reqitem_rptitem_tableData.length - 1].seq);
        }
      }
      for (var j = 0; j < this.copydata.length; j++) {
        if (insertRecords.filter((m) => m.rpt_itemcode == this.copydata[j].rpt_itemcode).length > 0) {
          continue;
        }
        if (this.reqitem_rptitem_tableData.filter((m) => m.rpt_itemcode == this.copydata[j].rpt_itemcode).length > 0) {
          continue;
        }
        this.copydata[j].req_itemcode = this.item_dict_currentRow.req_itemcode;
        this.copydata[j].seq = maxseq + 10;
        maxseq = this.copydata[j].seq;
        this.$refs.reqitem_rptitem_table.insert(this.copydata[j]);
      }
      this.reqitem_rptitem_SaveEvent();
    }, //双击开窗
    async showDurationFinishReport({ row, column }) {
      if (column.property == "req_itemname") {
        this.timespan = 10;
        this.QueryDurationFinishReport();
      } else if (column.property == "specimenname") {
        this.poprow_specimen = row;
        this.datatype = "specimen";
        this.headtitle = "标本类型";
        this.dialogVisible = true;
      }
    },
    async QueryDurationFinishReport() {
      try {
        const res = await this.$tool.request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          timeout: 600000,
          data: {
            action: "QueryDurationFinishReport",
            req_itemcode: this.item_dict_currentRow.req_itemcode,
            timespan: this.timespan,
          },
        });
        this.FinshReportData = res.data;
        this.dialogFinshReport = true;
      } catch (e) {
        this.$alert(e, "信息");
      }
    },
    dialogFinshReportclose() {
      this.dialogFinshReport = false;
    },
    FinshReportChk(row) {
      if (row.ischk == "1") {
        this.AddReqItem_RptItem(row);
      } else {
        row.chkflag = "0";
      }
    },
    dialogbtnAdd() {
      let data = this.$refs.detailXtable.getTableData().fullData;
      let seqlist = XE.max(data, (item) => item.seq);
      let maxseq = 10;
      if (!!seqlist) {
        maxseq = seqlist.seq + 10;
      }
      this.$refs.detailXtable.insert({ rpt_itemcode: this.RowDetail.rpt_itemcode, req_itemcode: this.RowDetail.req_itemcode, seq: maxseq, specimen_code: "All", rptunitid: "-1" }).then(({ row }) => {
        this.$refs.detailXtable.setCurrentRow(row);
        this.$refs.detailXtable.scrollToRow(row);
      });
    },
    dialogbtnDel() {
      try {
        this.$confirm("您确定删除这条数据吗？", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            var delrow = this.$refs.detailXtable.getCurrentRow();
            if (delrow) {
              var alld = this.$refs.detailXtable.tableData;
              var rowindex = alld.indexOf(delrow);
              this.$refs.detailXtable.remove(delrow);
              this.exesave("D").then((res) => {
                if (res.errcode == "1") {
                  this.$pub.message(this, "删除成功！", "success");
                  let havedata = this.$refs.detailXtable.tableData;
                  this.reqitem_rptitem_refresh();
                  if (havedata.length > 0) {
                    if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                    let selectrow = havedata[rowindex];
                    this.$refs.detailXtable.setCurrentRow(selectrow);
                  } else {
                    this.$refs.detailXtable.setCurrentRow(null);
                  }
                }
              });
            }
          })
          .catch(() => { });
      } catch (e) {
        console.log("error", e);
      }
    },

    async exesave(op) {
      try {
        let data = this.$refs.detailXtable.tableData;
        if (op == "S") {
          let kdata = [];
          let key = "";
          data.forEach((element) => {
            let key = element.specimen_code + "$" + element.rptunitid;
            kdata.push(key);
          });

          let kdata2 = XE.uniq(kdata);
          if (kdata2.length != kdata.length) {
            this.$alert("数据中不能重复保存相同的标本类型和报告单元！", "信息");
            return false;
          }
        }

        const res = await this.$tool.request({
          url: "LIS/LIS1/LIS113",
          method: "post",
          data: {
            action: "savedetail",
            savinfo: this.$refs.detailXtable.getRecordset(),
          },
        });
        return res;
      } catch (e) {
        this.$alert(e, "信息");
      }
    },
    isRepeat(arr) {
      var hash = {};
      for (var i in arr) {
        if (hash[arr[i]]) {
          return true;
        }
        // 不存在该元素，则赋值为true，可以赋任意值，相应的修改if判断条件即可
        hash[arr[i]] = true;
      }
      return false;
    },
    async savedetail() {
      try {
        const res = await this.exesave("S");
        if (res) {
          if (res.errcode == "1") {
            this.$pub.message(this, "保存成功！", "success");
            this.dialogdetail = false;
            this.reqitem_rptitem_refresh();
          } else {
            this.$alert("保存失败！" + res.errmsg);
          }
        }
      } catch (e) {
        this.$alert(e, "信息");
      }
    },
    popcomchange(code, name) {
      this.poprow_specimen.specimen_code = code;
      this.poprow_specimen.specimenname = name;
      this.dialogVisible = false;
    },
  },
};
</script>

<style scoped>
.pagetop {
  left: 0;
  right: 0;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 5px;
  overflow: auto;
}

.middle-left {
  position: absolute;
  top: 0px;
  bottom: 0px;
  width: 400px;
  left: 0px;
}

.middle-right {
  position: absolute;
  top: 0px;
  bottom: 0px;
  width: 400px;
  right: 0px;
}

.child_exteral {
  height: 100%;
  width: 400px;
  position: relative;
}

.child_bottom {
  position: absolute;
  top: 33px;
  width: 100%;
  bottom: 0px;
}

/deep/ .vxe-table .vxe-body--row.col-red {
  color: red;
}

/deep/ .vxe-table .vxe-body--row.col-grey {
  color: rgb(197, 189, 189);
}

table tr td:nth-child(odd) {
  text-align: right;
  white-space: nowrap;
}

table tr td:nth-child(even) {
  text-align: left;
}

table tr td:nth-child(1) {
  width: 80px;
}

table {
  font-size: 12px;
}

.table1style tr td {
  padding: 0px 2px 0px 5px;
}

.el-input-number--small {
  width: 130px;
  line-height: 23px;
}</style>